import { objDeepCopy,getDateString } from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.initdata();
	},
	mounted() {
		this.init();
	},
	activated() {
		this.init();
		this.initdata();
	},
	methods: {
		init() {
			this.Model.Outofstock = this.$route.query.flag;
			this.Model.OutofstockBtn = this.$route.query.status;
		},
		initdata() {
			this.Model.MainId = this.$route.query.id;		//订单id
			this.requestDetail(this,this.Model.MainId);
		},
		//详情接口
		requestDetail(that,id,pageIndex = 1,pageSize = 10) {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whInventoryDetails/list',
				// url:'/api/whInventoryDetails/list',
				data:{
					condition:{
						whInventoryMainId:id
					},
					pageIndex: pageIndex,
					pageSize: pageSize,
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
                    that.Model.fiNum = result.hasBeentotal;//已入条目
                    that.Model.allNum = result.total;
                    that.Model.stepOption.activeStep = result.inventoryStatus - 1;//状态
                    //当前状态的时间
                    that.Model.stepOption.step.forEach((val,idx)=>{
                        if(result.inventoryStatus - 1 == idx){
                            val.description = result.updateTime;
                        }
                    })
                    that.Model.tableData = [];
					//盘点单基础信息
					that.Model.tableFormInline = [{
							label: '盘点单号',
							value: result.countingNumber
						},
						{
							label: 'sap盘点单号',
							value: result.sapCountingNumber
						},
						{
							label: '盘点仓库',
							value: result.inventoryWarehouse
						},
						{
							label: '创建人',
							value: result.createBy
						},
						{
							label: '开始盘点时间',
							value: result.inventoryStartTime
						}
                    ];
                    var obj = {};
                    result.detailsList.forEach(items=>{
                        obj = objDeepCopy(items);
                        if(items.countingStatus == 1){
                            obj.ToEdit = 0;   //可编辑
                        }else{
                            obj.ToEdit = 1;   //不可编辑
                        }
                        that.Model.tableData.push(obj);
                    });
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//请求扫码入库 
		skuEntryAjax(that,code){
            var fiNum = this.Model.fiNum;
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whInventoryDetails/accordingToBarcodeComparison'+fiNum,
				// url:'/api/whInventoryDetails/accordingToBarcodeComparison/'+fiNum,
				data:{
                    keyword:code,
                    whInventoryMainId:this.Model.MainId
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					if(result){
                        this.Model.fiNum = result.totalNumberOfSolids;
                        var dataArr=[];
                        this.Model.tableData.forEach((val,idx,arr)=>{
                            //已有的条码
                            if(val.lotNumber == code || val.goodsNumber == code){
                                dataArr.push(val);
                                if(dataArr.length>0){
                                    this.Model.tableData.splice(idx,1);
                                    result.inventoryDetails.ToEdit = 0;//扫完变成可编辑
                                    this.Model.tableData.unshift(result.inventoryDetails);
                                }else{
                                    result.inventoryDetails.ToEdit = 0;//扫完变成可编辑
                                    this.Model.tableData.push(result.inventoryDetails);
                                } 
                            }
                        });
					}
				}else{
                    that.tips(that,'warning',data.msg)
				}
		
			});
		},
		
		//扫码入库
		checkSku(){
			var code = this.Model.writeSku;
			this.skuEntryAjax(this,code);
		},
		//保存、结束盘点
		inventoryHandle(type) {
            var mainTableData = this.Model.tableData;
            var Nonumberarr = '';//数量小于0的字符串
            var arr = [];
			mainTableData.forEach(item=>{
				if(item.numberOfSolidOrders <= 0 && item.countingStatus == 1){
					Nonumberarr += item.goodsNumber+'<br/>';
                }else{
                    var obj = {
                        id: item.id,
                        countingResults: item.countingResults,
                        numberOfDiscs: item.numberOfDiscs,
                        numberOfSolidOrders: item.numberOfSolidOrders,
                        whInventoryMainId: this.Model.MainId,
                        lotNumber: item.lotNumber,
                        goodsNumber: item.goodsNumber,
                        remark: item.remark
                    }
                    arr.push(obj);
                }
			});
            if (type == '结束盘点') {
                if(!Nonumberarr==''){
					this.$alert('<h4>以下商品编码未填写数量</h4><p>'+Nonumberarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.inventoryAjax(arr, 1, '已结束盘点');
				}
            } else {
                if(!Nonumberarr==''){
					this.$alert('<h4>以下商品编码未填写数量</h4><p>'+Nonumberarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.inventoryAjax(arr, 0, '保存成功');
				}  
            }	
		},
		//保存、结束盘点请求
		inventoryAjax(params,whetherToEnd,word) {
            var Solids = this.Model.fiNum;
			var that = this;
			this.$ajax({
				method: 'post',
				url:this.apiTest+`/supplier/whInventoryDetails/saveCountData/${whetherToEnd}`,
				// url:`/api/whInventoryDetails/saveCountData/${whetherToEnd}`,
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
                    if(word == '已结束盘点'){
                        that.$router.push({
                            path:'goodsInventory'
                        });
                    }
                    that.tips(that,'success',word)
					setTimeout(function(){
						that.Model.Outofstock = 1;
					},1000)
					
				}	
			});
        },
        //提交报告
        submitReport() {
            var submitter = 1;
            var MainId = this.Model.MainId;
			var that = this;
			this.$ajax({
				method: 'post',
				url:this.apiTest+`/supplier/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`,
				//url:`/api/whInventoryDetails/submitInventoryReport/${MainId}/${submitter}`
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
                    that.tips(that,'success','提交报告成功')
				}	
			});
        },
		//取消
		goBack() {
			this.$router.back(-1);
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		materialSelectionChange(val) {
			console.log(val);
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		handleChange(row) {
			this.Model.TableSelectData = row;
			console.log(row)
		},	
		//开始盘点
		BegainInventory() {
			this.Model.Outofstock = '0';
			this.Model.OutofstockBtn = '1';
		},
		//展开收缩
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
    },
    filters:{
        //结果
		ResultsWord:function(value){
			var val;
			switch (value) {
				case 1:
					val = '盘盈'
					break;
				case 2:
					val = '无差异'
					break;
				case 3:
					val = '盘亏'
					break;
				default:
					break;
			}
			return val;
        },
        //状态
        StatusWord:function(value){
			var val;
			switch (value) {
				case 0:
					val = ''
					break;
				case 1:
					val = '已盘'
					break;
				default:
					break;
			}
			return val;
		},
	}
}
export default Controller;
